#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>

#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)

int set_disp_mode(int fd, int option){
    int err;
    struct termios term;
    if(tcgetattr(fd, &term) == -1){
        perror("can not get the attribution of the terminal");
        return 1;
    }
    if(option){
        term.c_lflag |= ECHOFLAGS;
    }
    else{
        term.c_lflag &= ~ECHOFLAGS;
    }
    err = tcsetattr(fd, TCSAFLUSH, &term);
    if(err == -1 && err == EINTR){
        perror("can not set the attribution of the terminal");
        return 1;
    }
    return 0;
}


int main(){
    setbuf(stdout, NULL);
    printf("-----start test-------\n");
    set_disp_mode(STDIN_FILENO, 0);
    char c;
    for(;;){
        c = getchar();
        if(c == 'q'){
            break;
        }
        if(c == ' '){
            write(STDOUT_FILENO, &c ,sizeof(c));
        }
        if(c == '\n'){
            write(STDOUT_FILENO, &c ,sizeof(c));
        }
    }
    set_disp_mode(STDIN_FILENO, 1);
    printf("-----finish test-------\n");
    return 0;
}